package com.qigao.mall.service.channel;

import com.qigao.mall.api.ChannelRouteService;
import com.qigao.mall.commons.enums.StatusEnum;
import com.qigao.mall.commons.enums.WebSourceEnum;
import com.qigao.mall.commons.enums.YesNoEnum;
import com.qigao.mall.commons.utils.PairString;
import com.qigao.mall.commons.utils.PayUtil;
import com.qigao.mall.commons.utils.RSAUtils;
import com.qigao.mall.jdbc.mapper.TbChannelInfoMapper;
import com.qigao.mall.jdbc.mapper.TbMerProductMapper;
import com.qigao.mall.jdbc.mapper.TbProductChannelMapper;
import com.qigao.mall.jdbc.mapper.TbShopInfoMapper;
import com.qigao.mall.jdbc.model.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * @author luiz
 * @Title: com.qigao.mall.service.channel
 * @ProjectName three-mall
 * @date 2020-03-2020/3/12 15:27
 * @Description:
 */
@Service
@Slf4j
public class ChannelRouteServiceImpl implements ChannelRouteService {
    @Resource
    private TbMerProductMapper tbMerProductMapper;
    @Resource
    private TbProductChannelMapper tbProductChannelMapper;
    @Resource
    private TbChannelInfoMapper tbChannelInfoMapper;
    @Resource
    private TbShopInfoMapper tbShopInfoMapper;
    @Override
    public boolean verifySign(Object respData, String webSource) {
        if(WebSourceEnum.WEICHAT_JSAPI.getCode().equals(webSource)){
           return verifyWeichat((Map)respData);
        }else{
            return verifyAliPay((Map)respData);
        }
    }

    private boolean verifyAliPay(Map respMap) {
        String respStr= PairString.createLinkString(respMap);
        String signValue=(String) respMap.get("sign");
        respMap.remove("sign");
        respMap.remove("sign_type");
        String outPartenrNo=(String)respMap.get("mch_id");//支付宝端商户号
        TbChannelInfo tbChannelInfo=tbChannelInfoMapper.selectByPartenerNo(outPartenrNo);
        return RSAUtils.verifyWithRsa2(respStr,signValue,tbChannelInfo.getPubKey());
    }

    private boolean verifyWeichat(Map respMap) {
        String signStr=(String) respMap.get("sign");
        respMap.remove("sign");
        String outPartenrNo=(String)respMap.get("mch_id");//微信端商户号
        TbChannelInfo tbChannelInfo=tbChannelInfoMapper.selectByPartenerNo(outPartenrNo);
        String genSign= PayUtil.createSign(respMap,tbChannelInfo.getSignType(),tbChannelInfo.getMd5Key());
        if(!signStr.equals(genSign)){
            return false;
        }
        return true;
    }

    @Override
    public String decryptData(Map<String, String> respMap, String webSource) {
        if(WebSourceEnum.WEICHAT_JSAPI.getCode().equals(webSource)){
            return decryptWeichatData(respMap);
        }
        return null;
    }

    private String decryptWeichatData(Map<String, String> respMap) {
        String paySign=(String) respMap.get("req_info");
        String outPartenrNo=(String)respMap.get("mch_id");//微信端商户号
        TbChannelInfo tbChannelInfo=tbChannelInfoMapper.selectByPartenerNo(outPartenrNo);
        String refundNotifyData= PayUtil.decryptData(paySign,tbChannelInfo.getMd5Key());
        return refundNotifyData;
    }

    @Override
    public TbMerChannel findChannelRouteShop(String shopNo, String webSource, String payWayCode) {
        TbShopInfo tbShopInfo=tbShopInfoMapper.selectByShopNo(shopNo);
        if(tbShopInfo==null|| StatusEnum.VALID.getCode()!=tbShopInfo.getValidStatus()){
            log.warn("店铺信息无效!{}",shopNo);
            return null;
        }
        return findChannelRoute(tbShopInfo.getMerNo(),webSource,payWayCode);
    }

    @Override
    public TbMerChannel findChannelRoute(String merNo, String webSource, String payWay) {
        TbMerProduct tbMerProduct=tbMerProductMapper.selectByIndex(merNo,webSource,payWay);
        if(tbMerProduct==null|| !YesNoEnum.YES.getCode().equals(tbMerProduct.getStatus())){
           log.warn("商户产品配置不存在!{},{},{}",merNo,webSource,payWay);
            return null;
        }
        List<TbProductChannel> tbProductChannels=tbProductChannelMapper.selectByMerNoAndProductNo(merNo,tbMerProduct.getProductNo());
        if(tbProductChannels==null||tbProductChannels.size()<=0){
            log.warn("产品对应的路由配置不存在!{}",tbMerProduct.getProductNo());
            return null;
        }
        tbProductChannels.sort(new Comparator<TbProductChannel>() {
            @Override
            public int compare(TbProductChannel o1, TbProductChannel o2) {
                return o1.getPriorLevel()-o2.getPriorLevel();
            }
        });
        TbProductChannel tbProductChannel=null;
        for(TbProductChannel tmpProductChannel:tbProductChannels){
            if(YesNoEnum.YES.getCode().equals(tmpProductChannel.getStatus())){
                tbProductChannel = tmpProductChannel;
            }
        }
        if(tbProductChannel==null){
            log.warn("产品对应的路由配置都无效!{}",tbMerProduct.getProductNo());
            return null;
        }
        TbChannelInfo tbChannelInfo=tbChannelInfoMapper.selectByPartenerNo(tbProductChannel.getChannelPartenerNo());
        TbMerChannel tbMerChannel=new TbMerChannel();
        BeanUtils.copyProperties(tbMerProduct,tbMerChannel);
        BeanUtils.copyProperties(tbProductChannel,tbMerChannel);
        BeanUtils.copyProperties(tbChannelInfo,tbMerChannel);
        return tbMerChannel;
    }


}
